package com.coffee.daili.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.supplier.query.SupplierQuery;
import com.coffee.admin.service.common.bean.GtRegion;
import com.coffee.admin.service.common.dao.GtRegionDao;
import com.coffee.admin.service.merchant.bean.Merchant;
import com.coffee.admin.service.merchant.dao.MerchantDao;
import com.coffee.admin.service.supplier.bean.Supplier;
import com.coffee.admin.service.supplier.dao.SupplierDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.DailiMerchantRoleEnum;
import com.coffee.core.enums.DailiMerchantStatusEnum;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.DateUtil;
import com.coffee.core.util.StringUtil;
import com.coffee.daili.api.query.DailiOrderQuery;
import com.coffee.daili.api.query.DailiMerchantQuery;
import com.coffee.daili.api.result.*;
import com.coffee.daili.api.service.DailiOrderService;
import com.coffee.daili.service.bean.DailiOrder;
import com.coffee.daili.service.bean.DailiMerchant;
import com.coffee.daili.service.dao.DailiMerchantDao;
import com.coffee.daili.service.dao.DailiOrderDao;

import com.coffee.daili.service.dao.DailiMerchantDao;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class DailiOrderServiceImpl implements DailiOrderService {
    private final static Logger logger = LoggerFactory.getLogger(DailiOrderServiceImpl.class);
    @Resource
    private DailiOrderDao dailiOrderDao;
    @Resource
    private DailiMerchantDao daliMerchantDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private MerchantDao merchantDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private GtRegionDao gtRegionDao;

    @Override
    public Page<DailiOrderResult> getPager(DailiOrderQuery param, PagerInfo pr) {
        Page<DailiOrderResult> pager = new Page<DailiOrderResult>(pr.getStart(), pr.getPageSize());
        Integer count = dailiOrderDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<DailiOrder> list = dailiOrderDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, DailiOrderResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(DailiOrderQuery param) {
        Integer count = dailiOrderDao.count(param);
        return count;
    }

    @Override
    public DailiOrderResult getDetail(Integer id) {
        DailiOrder item = dailiOrderDao.findById(id);
        if (item == null) {
            return null;
        }
        DailiOrderResult result = new DailiOrderResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<DailiOrderResult> getList(DailiOrderQuery param, PagerInfo pr) {
        List<DailiOrder> list = dailiOrderDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, DailiOrderResult.class);
    }

    @Override
    public List<DailiOrderResult> getList(DailiOrderQuery param) {
        List<DailiOrder> list = dailiOrderDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, DailiOrderResult.class);
    }

    @Override
    public List<DailiOrderResult> getList() {
        DailiOrderQuery param = new DailiOrderQuery();
        List<DailiOrder> list = dailiOrderDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, DailiOrderResult.class);
    }


    @Override
    public void delete(Integer id) {
        dailiOrderDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(DailiOrderResult result) throws BusinessException {
        DailiOrder item = new DailiOrder();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            dailiOrderDao.insert(item);
        } else {
            DailiOrder tmp = dailiOrderDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            dailiOrderDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, DailiOrderResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, DailiOrderResult> map = new HashMap<Integer, DailiOrderResult>();
        List<DailiOrder> list = dailiOrderDao.findByIds(StringUtil.setToList(idSet));
        List<DailiOrderResult> resultList = DTOUtils.convertList(list, DailiOrderResult.class);
        for (DailiOrderResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, DailiOrderResult> getItemMap() {
        Map<Integer, DailiOrderResult> map = new HashMap<Integer, DailiOrderResult>();
        DailiOrderQuery param = new DailiOrderQuery();
        List<DailiOrder> list = dailiOrderDao.page(param, 0, Integer.MAX_VALUE);
        List<DailiOrderResult> resultList = DTOUtils.convertList(list, DailiOrderResult.class);
        for (DailiOrderResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


    @Override
    public DailiMerchantOrderResult getDailiMerchantOrderResult(Integer merchantId, String date, Integer dailiMerchantId) {
        DailiMerchantOrderResult dailiMerchantOrderResult = new DailiMerchantOrderResult();
        Merchant merchant = merchantDao.findById(merchantId);
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(merchant.getMobile());
        DailiMerchant daliMerchant = daliMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null || !"Y".equals(daliMerchant.getIsEnable())) {
            return null;
        }
        if (dailiMerchantId != null) {
            daliMerchant = daliMerchantDao.findById(dailiMerchantId);
        }
        logger.info("daliMerchant====" + JSONObject.toJSONString(daliMerchant));
        Map<String, Object> map = dailiOrderDao.monthCount(date, daliMerchant.getPidPath());
        Number total = (Number) map.get("total");
        Number amount = (Number) map.get("amount");
        dailiMerchantOrderResult.setMonthCount(total.intValue());
        dailiMerchantOrderResult.setMonthAmount(amount.doubleValue());
        Map<String, Object> map1 = dailiOrderDao.allCount(daliMerchant.getPidPath());
        Number total1 = (Number) map1.get("total");
        dailiMerchantOrderResult.setCount(total1.intValue());
        Number amount1 = (Number) map1.get("amount");
        dailiMerchantOrderResult.setAmount(amount1.doubleValue());
        if (dailiMerchantOrderResult.getQuota() != null && DailiMerchantRoleEnum.PARTNER.getCode().equals(daliMerchant.getRole())) {
            Double quota = daliMerchantDao.findQuotaByPid(daliMerchant.getPidPath());
            dailiMerchantOrderResult.setQuota(daliMerchant.getQuota());
            dailiMerchantOrderResult.setRemainQuota(dailiMerchantOrderResult.getQuota() - quota);
        }
        DailiSupplierSubOrderResult dailiSupplierSubOrderResult = dailiOrderDao.getDailiSupplierSubOrderResultByTotal(date, null, null, daliMerchant.getPidPath());
        DailiSupplierSubOrderResult dailiSupplierSubOrderResultByEnjoyWait = dailiOrderDao.getDailiSupplierSubOrderResultByEnjoyWait(date, null, null, daliMerchant.getPidPath());
        DailiSupplierSubOrderResult dailiSupplierSubOrderResultByEnjoySuccess = dailiOrderDao.getDailiSupplierSubOrderResultByEnjoySuccess(date, null, null, daliMerchant.getPidPath());
        Integer count1 = dailiOrderDao.getDailiOrderListCount(date, null, null, daliMerchant.getPidPath());
        logger.info("count1====" + JSONObject.toJSONString(count1));
        logger.info("dailiSupplierSubOrderResult====" + JSONObject.toJSONString(dailiSupplierSubOrderResult));
        logger.info("dailiSupplierSubOrderResultByEnjoyWait====" + JSONObject.toJSONString(dailiSupplierSubOrderResultByEnjoyWait));
        logger.info("dailiSupplierSubOrderResultByEnjoySuccess====" + JSONObject.toJSONString(dailiSupplierSubOrderResultByEnjoySuccess));
        dailiMerchantOrderResult.setOrderCount(count1);
        dailiMerchantOrderResult.setTotalAmount(dailiSupplierSubOrderResult.getTotalAmount());
        dailiMerchantOrderResult.setFengxiaoyuanAmount(dailiSupplierSubOrderResultByEnjoySuccess.getFengxiaoyuanAmount());
        dailiMerchantOrderResult.setFengyongAmount(dailiSupplierSubOrderResultByEnjoySuccess.getFengyongAmount());
        dailiMerchantOrderResult.setFengxiaoyuanAmountByExpect(dailiSupplierSubOrderResultByEnjoyWait.getFengxiaoyuanAmount());
        dailiMerchantOrderResult.setFengyongAmountByExpect(dailiSupplierSubOrderResultByEnjoyWait.getFengyongAmount());
        return dailiMerchantOrderResult;
    }

    @Override
    public Page<DailiOrderPageResult> getDailiOrderPageResultPager(String date, Integer merchantId, Integer dailiMerchantId, PagerInfo pr) {
        Page<DailiOrderPageResult> pager = new Page<DailiOrderPageResult>(pr.getStart(), pr.getPageSize());
        Merchant merchant = merchantDao.findById(merchantId);
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(merchant.getMobile());
        DailiMerchant daliMerchant = daliMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null) {
            return null;
        }
        Integer dailiOrderPageResultCount = dailiOrderDao.getDailiOrderPageResultCount(date, daliMerchant.getPidPath(), dailiMerchantId);
        if (dailiOrderPageResultCount > 0) {
            List<DailiOrderPageResult> dailiOrderPageResultPage = dailiOrderDao.getDailiOrderPageResultPage(date, daliMerchant.getPidPath(), dailiMerchantId, pr.getStart(), pr.getPageSize());
            for (DailiOrderPageResult dailiOrderPageResult : dailiOrderPageResultPage) {
                SkuCategory skuCategory = skuCategoryDao.findById(dailiOrderPageResult.getCategoryId());
                if (skuCategory != null) {
                    dailiOrderPageResult.setCategoryName(skuCategory.getName());
                }
            }
            pager.setData(dailiOrderPageResultPage);
        }
        pager.setTotalPage(dailiOrderPageResultCount);
        return pager;
    }

    @Override
    public DailiMerchantResult getDailiMerchatResult(Integer supplierId, String date) {
        DailiMerchantResult dailiMerchantResult = new DailiMerchantResult();
        Supplier supplier = supplierDao.findById(supplierId);
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(supplier.getMobile());
        DailiMerchant daliMerchant = daliMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null) {
            return null;
        }
        BeanUtilExt.copyProperties(dailiMerchantResult, daliMerchant);
        DailiSupplierSubOrderResult dailiSupplierSubOrderResult = dailiOrderDao.getDailiSupplierSubOrderResult(date, null, null, daliMerchant.getPidPath());
        dailiMerchantResult.setFengxiaoyuanAmount(dailiSupplierSubOrderResult.getFengxiaoyuanAmount());
        dailiMerchantResult.setFengyongAmount(dailiSupplierSubOrderResult.getFengyongAmount());
        dailiMerchantResult.setBusinessName(supplier.getBusinessName());
        dailiMerchantResult.setBusinessCode(supplier.getBusinessCode());
        dailiMerchantResult.setName(supplier.getName());
        if (daliMerchant.getProvinceId() != null) {
            GtRegion province = gtRegionDao.findById(daliMerchant.getProvinceId());
            if (province != null)
                dailiMerchantResult.setProvinceName(province.getName());
        }
        if (daliMerchant.getCityId() != null) {
            GtRegion city = gtRegionDao.findById(daliMerchant.getCityId());
            if (city != null)
                dailiMerchantResult.setCityName(city.getName());
        }
        if (daliMerchant.getAreaId() != null) {
            GtRegion area = gtRegionDao.findById(daliMerchant.getAreaId());
            if (area != null)
                dailiMerchantResult.setAreaName(area.getName());
        }
        return dailiMerchantResult;
    }

    @Override
    public Page<FenxiaoYuanList> getFenxiaoYuanList(Integer supplierId, String date, PagerInfo pr) {
        Page<FenxiaoYuanList> page = new Page(pr.getPageIndex(), pr.getPageSize());
        Supplier supplier = supplierDao.findById(supplierId);
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(supplier.getMobile());
        DailiMerchant daliMerchant = daliMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null) {
            return null;
        }
        DailiMerchantQuery listParam = new DailiMerchantQuery();
        listParam.setPid(daliMerchant.getId());
        listParam.setRole(DailiMerchantRoleEnum.DISTRIBUTOR.getCode());
        Integer count1 = daliMerchantDao.count(listParam);
        if (count1 > 0) {
            List<DailiMerchant> list = daliMerchantDao.list(listParam);
            List<FenxiaoYuanList> fenxiaoYuanLists = DTOUtils.convertList(list, FenxiaoYuanList.class);
            for (FenxiaoYuanList fenxiaoYuanList : fenxiaoYuanLists) {
                DailiMerchantQuery fenxiaoYuanParam = new DailiMerchantQuery();
                fenxiaoYuanParam.setPid(fenxiaoYuanList.getId());
                fenxiaoYuanParam.setRole(DailiMerchantRoleEnum.MERCHANT.getCode());
                Integer count = daliMerchantDao.count(fenxiaoYuanParam);
                fenxiaoYuanList.setMerchantCount(count);
                Map<String, Object> map = dailiOrderDao.monthCount(date, fenxiaoYuanList.getPidPath());
                Number total = (Number) map.get("total");
                Number amount = (Number) map.get("amount");
                fenxiaoYuanList.setOrderCount(total.intValue());
                fenxiaoYuanList.setFengxiaoyuanAmount(amount.doubleValue());
            }
            page.setData(fenxiaoYuanLists);
        }
        page.setTotalPage(count1);
        return page;
    }

    @Override
    public Page<ShanghuListResult> getShanghuListResult(Integer supplierId, PagerInfo pr) {
        Page<ShanghuListResult> page = new Page(pr.getPageIndex(), pr.getPageSize());
        Supplier supplier = supplierDao.findById(supplierId);
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(supplier.getMobile());
        DailiMerchant daliMerchant = daliMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null) {
            return null;
        }
        DailiMerchantQuery listParam = new DailiMerchantQuery();
        listParam.setPidPath(daliMerchant.getPidPath());
        listParam.setRole(DailiMerchantRoleEnum.MERCHANT.getCode());
        Integer count1 = daliMerchantDao.count(listParam);
        if (count1 > 0) {
            List<DailiMerchant> list = daliMerchantDao.list(listParam);
            List<ShanghuListResult> shanghuLists = DTOUtils.convertList(list, ShanghuListResult.class);
            for (ShanghuListResult shanghulist : shanghuLists) {
                DailiMerchant daliMerchantDaoById = daliMerchantDao.findById(shanghulist.getPid());
                if (daliMerchantDaoById != null) {
                    if (DailiMerchantRoleEnum.PARTNER.getCode().equals(daliMerchantDaoById.getRole())) {
                        shanghulist.setPartner(daliMerchantDaoById.getName());
                    } else if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(daliMerchantDaoById.getRole())) {
                        shanghulist.setDistributor(daliMerchantDaoById.getName());
                    }
                }

            }
            page.setData(shanghuLists);
        }
        page.setTotalPage(count1);
        return page;
    }

    @Override
    public Page<DailiOrderListResult> getDailiOrderListResult(Integer supplierId, String date, PagerInfo pr) {
        Page<DailiOrderListResult> page = new Page(pr.getPageIndex(), pr.getPageSize());
        Supplier supplier = supplierDao.findById(supplierId);
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(supplier.getMobile());
        DailiMerchant daliMerchant = daliMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null) {
            return null;
        }
        Integer count1 = dailiOrderDao.getDailiOrderListCount(date, null, null, daliMerchant.getPidPath());
        if (count1 > 0) {
            List<DailiOrderListResult> dailiOrderListResultList = dailiOrderDao.getDailiOrderListResult(null,date, null, null, daliMerchant.getPidPath(), pr.getStart(), pr.getPageSize());
            for (DailiOrderListResult dailiOrderListResult : dailiOrderListResultList) {
                DailiMerchant dailiOrderMerchant = daliMerchantDao.findById(dailiOrderListResult.getDailiMerchantId());
                if (dailiOrderMerchant != null) {
                    dailiOrderListResult.setName(dailiOrderMerchant.getName());
                    dailiOrderListResult.setMobile(dailiOrderMerchant.getMobile());
                    if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(dailiOrderMerchant.getRole())) {
                        dailiOrderListResult.setDistributor(dailiOrderMerchant.getName());
                    } else if (DailiMerchantRoleEnum.MERCHANT.getCode().equals(dailiOrderMerchant.getRole())) {
                        DailiMerchant dailiPidMerchant = daliMerchantDao.findById(dailiOrderMerchant.getPid());
                        if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(dailiPidMerchant.getRole())) {
                            dailiOrderListResult.setDistributor(dailiPidMerchant.getName());
                        }
                    }
                }

            }
            page.setData(dailiOrderListResultList);
        }
        page.setTotalPage(count1);
        return page;
    }

    @Override
    public DailiMerchantResult getDailiMerchatResultByDailiMerchantId(Integer dailiMerchantId, String date, Date startTime, Date endTime) {
        DailiMerchantResult dailiMerchantResult = new DailiMerchantResult();
        DailiMerchant daliMerchant = daliMerchantDao.findById(dailiMerchantId);
        if (daliMerchant == null) {
            return null;
        }
        BeanUtilExt.copyProperties(dailiMerchantResult, daliMerchant);
        DailiSupplierSubOrderResult dailiSupplierSubOrderResult = dailiOrderDao.getDailiSupplierSubOrderResultByTotal(date, startTime, endTime, daliMerchant.getPidPath());
        DailiSupplierSubOrderResult dailiSupplierSubOrderResultByEnjoyWait = dailiOrderDao.getDailiSupplierSubOrderResultByEnjoyWait(date, startTime, endTime, daliMerchant.getPidPath());
        DailiSupplierSubOrderResult dailiSupplierSubOrderResultByEnjoySuccess = dailiOrderDao.getDailiSupplierSubOrderResultByEnjoySuccess(date, startTime, endTime, daliMerchant.getPidPath());
        Integer count1 = dailiOrderDao.getDailiOrderListCount(null, startTime, endTime, daliMerchant.getPidPath());
        dailiMerchantResult.setOrderCount(count1);
        dailiMerchantResult.setTotalAmount(dailiSupplierSubOrderResult.getTotalAmount());
        dailiMerchantResult.setFengxiaoyuanAmount(dailiSupplierSubOrderResultByEnjoySuccess.getFengxiaoyuanAmount());
        dailiMerchantResult.setFengyongAmount(dailiSupplierSubOrderResultByEnjoySuccess.getFengyongAmount());
        dailiMerchantResult.setFengxiaoyuanAmountByExpect(dailiSupplierSubOrderResultByEnjoyWait.getFengxiaoyuanAmount());
        dailiMerchantResult.setFengyongAmountByExpect(dailiSupplierSubOrderResultByEnjoyWait.getFengyongAmount());
        SupplierQuery param = new SupplierQuery();
        param.setMobile(daliMerchant.getMobile());
        Supplier supplier = supplierDao.findFirst(param);
        if (supplier != null) {
            dailiMerchantResult.setBusinessName(supplier.getBusinessName());
            dailiMerchantResult.setBusinessCode(supplier.getBusinessCode());
            dailiMerchantResult.setName(supplier.getName());
        }
        if (daliMerchant.getProvinceId() != null) {
            GtRegion province = gtRegionDao.findById(daliMerchant.getProvinceId());
            if (province != null)
                dailiMerchantResult.setProvinceName(province.getName());
        }
        if (daliMerchant.getCityId() != null) {
            GtRegion city = gtRegionDao.findById(daliMerchant.getCityId());
            if (city != null)
                dailiMerchantResult.setCityName(city.getName());
        }
        if (daliMerchant.getAreaId() != null) {
            GtRegion area = gtRegionDao.findById(daliMerchant.getAreaId());
            if (area != null)
                dailiMerchantResult.setAreaName(area.getName());
        }
        return dailiMerchantResult;
    }

    @Override
    public Page<FenxiaoYuanList> getFenxiaoYuanListByDailiMerchantId(Integer dailiMerchantId, String date, PagerInfo pr) {
        Page<FenxiaoYuanList> page = new Page(pr.getPageIndex(), pr.getPageSize());
        DailiMerchantQuery listParam = new DailiMerchantQuery();
        listParam.setPid(dailiMerchantId);
        listParam.setRole(DailiMerchantRoleEnum.DISTRIBUTOR.getCode());
        Integer count1 = daliMerchantDao.count(listParam);
        if (count1 > 0) {
            List<DailiMerchant> list = daliMerchantDao.list(listParam);
            List<FenxiaoYuanList> fenxiaoYuanLists = DTOUtils.convertList(list, FenxiaoYuanList.class);
            for (FenxiaoYuanList fenxiaoYuanList : fenxiaoYuanLists) {
                DailiMerchantQuery fenxiaoYuanParam = new DailiMerchantQuery();
                fenxiaoYuanParam.setPid(fenxiaoYuanList.getId());
                fenxiaoYuanParam.setRole(DailiMerchantRoleEnum.MERCHANT.getCode());
                Integer count = daliMerchantDao.count(fenxiaoYuanParam);
                fenxiaoYuanList.setMerchantCount(count);
                Map<String, Object> map = dailiOrderDao.monthCount(date, fenxiaoYuanList.getPidPath());
                Number total = (Number) map.get("total");
                Number amount = (Number) map.get("amount");
                Number payAmount = (Number) map.get("payAmount");
                Number distributorCommission = (Number) map.get("distributorCommission");
                fenxiaoYuanList.setOrderCount(total.intValue());
                fenxiaoYuanList.setFengyongAmount(amount.doubleValue());
                fenxiaoYuanList.setFengxiaoyuanAmount(distributorCommission.doubleValue());
                fenxiaoYuanList.setTotalAmount(amount.doubleValue() + distributorCommission.doubleValue());
                fenxiaoYuanList.setPayAmount(payAmount.doubleValue());
            }
            page.setData(fenxiaoYuanLists);
        }
        page.setTotalPage(count1);
        return page;
    }

    @Override
    public Page<ShanghuListResult> getShanghuListResultByDailiMerchantId(Integer dailiMerchantId, PagerInfo pr) {
        Page<ShanghuListResult> page = new Page(pr.getPageIndex(), pr.getPageSize());
        DailiMerchant daliMerchant = daliMerchantDao.findById(dailiMerchantId);
        if (daliMerchant == null) {
            return null;
        }
        DailiMerchantQuery listParam = new DailiMerchantQuery();
        listParam.setPidPath(daliMerchant.getPidPath());
        listParam.setRole(DailiMerchantRoleEnum.MERCHANT.getCode());
        Integer count1 = daliMerchantDao.count(listParam);
        if (count1 > 0) {
            List<DailiMerchant> list = daliMerchantDao.list(listParam);
            List<ShanghuListResult> shanghuLists = DTOUtils.convertList(list, ShanghuListResult.class);
            for (ShanghuListResult shanghulist : shanghuLists) {
                DailiMerchant daliMerchantDaoById = daliMerchantDao.findById(shanghulist.getPid());
                if (daliMerchantDaoById != null) {
                    if (DailiMerchantRoleEnum.PARTNER.getCode().equals(daliMerchantDaoById.getRole())) {
                        shanghulist.setPartner(daliMerchantDaoById.getName());
                    } else if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(daliMerchantDaoById.getRole())) {
                        shanghulist.setDistributor(daliMerchantDaoById.getName());
                    }
                }

            }
            page.setData(shanghuLists);
        }
        page.setTotalPage(count1);
        return page;
    }

    @Override
    public Page<DailiOrderListResult> getDailiOrderListResultByDailiMerchantId(Integer dailiMerchantId, String date,String status, Date startTime, Date endTime, PagerInfo pr) {
        Page<DailiOrderListResult> page = new Page(pr.getPageIndex(), pr.getPageSize());
        DailiMerchant daliMerchant = daliMerchantDao.findById(dailiMerchantId);
        if (daliMerchant == null) {
            return page;
        }
        Integer count1 = dailiOrderDao.getDailiOrderListCount(date, startTime, endTime, daliMerchant.getPidPath());
        if (count1 > 0) {
            List<DailiOrderListResult> dailiOrderListResultList = dailiOrderDao.getDailiOrderListResult(status,date,startTime, endTime, daliMerchant.getPidPath(), pr.getStart(), pr.getPageSize());
            for (DailiOrderListResult dailiOrderListResult : dailiOrderListResultList) {
                DailiMerchant dailiOrderMerchant = daliMerchantDao.findById(dailiOrderListResult.getDailiMerchantId());
                if (dailiOrderMerchant != null) {
                    dailiOrderListResult.setName(dailiOrderMerchant.getName());
                    dailiOrderListResult.setMobile(dailiOrderMerchant.getMobile());
                    if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(dailiOrderMerchant.getRole())) {
                        dailiOrderListResult.setDistributor(dailiOrderMerchant.getName());
                    } else if (DailiMerchantRoleEnum.MERCHANT.getCode().equals(dailiOrderMerchant.getRole())) {
                        DailiMerchant dailiPidMerchant = daliMerchantDao.findById(dailiOrderMerchant.getPid());
                        if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(dailiPidMerchant.getRole())) {
                            dailiOrderListResult.setDistributor(dailiPidMerchant.getName());
                        }
                    }
                }

            }
            page.setData(dailiOrderListResultList);
        }
        page.setTotalPage(count1);
        return page;
    }

    @Override
    public Page<HehuorenListResult> getHehuorenListResult(String name, Integer provinceId, Integer cityId, Integer areaId, PagerInfo pr,Date startTime,Date endTime) {
        Page<HehuorenListResult> page = new Page<>(pr.getPageIndex(), pr.getPageSize());
        DailiMerchantQuery param = new DailiMerchantQuery();
        param.setName(name);
        param.setProvinceId(provinceId);
        param.setCityId(cityId);
        param.setAreaId(areaId);
        param.setRole(DailiMerchantRoleEnum.PARTNER.getCode());
        param.setStatus(DailiMerchantStatusEnum.SUCCESS.getCode());
        Integer count = daliMerchantDao.count(param);
        if (count > 0) {
            List<DailiMerchant> dailiMerchants = daliMerchantDao.page(param, pr.getStart(), pr.getPageSize());
            List<HehuorenListResult> hehuorenListResults = DTOUtils.convertList(dailiMerchants, HehuorenListResult.class);
            for (HehuorenListResult hehuorenListResult : hehuorenListResults) {
                SupplierQuery supplierQuery = new SupplierQuery();
                supplierQuery.setMobile(hehuorenListResult.getMobile());
                Supplier supplier = supplierDao.findFirst(supplierQuery);
                if (supplier != null) {
                    hehuorenListResult.setBusinessName(supplier.getBusinessName());
                    hehuorenListResult.setBusinessCode(supplier.getBusinessCode());
                    hehuorenListResult.setName(supplier.getName());
                }
                if (hehuorenListResult.getProvinceId() != null) {
                    GtRegion province = gtRegionDao.findById(hehuorenListResult.getProvinceId());
                    if (province != null)
                        hehuorenListResult.setProvinceName(province.getName());
                }
                if (hehuorenListResult.getCityId() != null) {
                    GtRegion city = gtRegionDao.findById(hehuorenListResult.getCityId());
                    if (city != null)
                        hehuorenListResult.setCityName(city.getName());
                }
                if (hehuorenListResult.getAreaId() != null) {
                    GtRegion area = gtRegionDao.findById(hehuorenListResult.getAreaId());
                    if (area != null)
                        hehuorenListResult.setAreaName(area.getName());
                }
                DailiMerchantQuery merchantParam = new DailiMerchantQuery();
                merchantParam.setPid(hehuorenListResult.getId());
                merchantParam.setRole(DailiMerchantRoleEnum.MERCHANT.getCode());
                Integer merchantCount = daliMerchantDao.count(merchantParam);
                hehuorenListResult.setMerchantCount(merchantCount);
                DailiMerchantQuery fenxiaoyuanParam = new DailiMerchantQuery();
                fenxiaoyuanParam.setPid(hehuorenListResult.getId());
                fenxiaoyuanParam.setRole(DailiMerchantRoleEnum.DISTRIBUTOR.getCode());
                Integer distributorCount = daliMerchantDao.count(fenxiaoyuanParam);
                hehuorenListResult.setFengxiaoyuanCount(distributorCount);
                Integer dailiOrderListCount = dailiOrderDao.getDailiOrderListCount(null, startTime, endTime,hehuorenListResult.getPidPath());
                hehuorenListResult.setOrderCount(dailiOrderListCount);
                DailiSupplierSubOrderResult dailiSupplierSubOrderResultByTotal = dailiOrderDao.getDailiSupplierSubOrderResultByTotal(null, startTime, endTime,hehuorenListResult.getPidPath());
                hehuorenListResult.setTotalAmount(dailiSupplierSubOrderResultByTotal.getTotalAmount());

            }
            page.setData(hehuorenListResults);
        }
        page.setTotalPage(count);
        return page;
    }
}
